import { Queue } from '@/lib/Queue'
import { IEvent } from "../index.d";
import useDrawLoop from "./useDrawLoop";

const useEventQueue = () => {
  const eventQueue = new Queue<IEvent>();
  const eventID: boolean[] = [];
  let enQueueNumber = 0;
  useDrawLoop(eventQueue, eventID)

  const add = (event: IEvent) => {
    console.log('add', enQueueNumber)
    event.id = enQueueNumber;
    eventID[enQueueNumber] = true;

    setTimeout(function(e: IEvent) {
      eventQueue.enqueue(e);
      e.eventStartTime = Date.now();
      console.log(e.id)
    }, event.delay, event);

    return enQueueNumber++;
  }

  const remove = (id: number) => {
    eventID[id] = false;
  }

  const isRun = (id: number) => {
    return eventID[id];
  }

  return {
    add, 
    remove,
    isRun,
  }
}

export default useEventQueue;